package com.yuanfeng.base.service.impl;

import com.alibaba.nacos.common.utils.CollectionUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import com.yuanfeng.base.dto.FloorDTO;
import com.yuanfeng.base.dto.FloorListDTO;
import com.yuanfeng.base.dto.GdbPcIndexInfoDTO;
import com.yuanfeng.base.entity.BaseFloorAdpageEntity;
import com.yuanfeng.base.entity.BasePcIndexConfigEntity;
import com.yuanfeng.base.mapper.BaseFloorAdpageMapper;
import com.yuanfeng.base.mapper.BasePcIndexConfigMapper;
import com.yuanfeng.base.service.BaseFloorAdpageService;
import com.yuanfeng.base.vo.index.GdbPcIndexVO;
import com.yuanfeng.commoms.constant.CommonType;
import com.yuanfeng.commoms.exception.BizCodeEnume;
import com.yuanfeng.commoms.util.Json.JsonUtils;
import com.yuanfeng.commoms.util.RedisPrefixUtils;
import com.yuanfeng.commoms.util.RedisUtil;
import com.yuanfeng.commoms.util.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.util.Date;
import java.util.List;

/**
 *
 */
@Service
public class BaseFloorAdpageServiceImpl extends ServiceImpl<BaseFloorAdpageMapper, BaseFloorAdpageEntity>
    implements BaseFloorAdpageService {

    @Value("${system.pcIndexConfig}")
    private  String pcIndexConfig ;

    private static String pcIndexConfig2;


    @PostConstruct
    public  String getPcIndexConfig2() {
        return pcIndexConfig2 = this.pcIndexConfig;
    }

    @Autowired
    private BasePcIndexConfigMapper basePcIndexConfigMapper;
    @Override
    public ResponseResult updateQueryPcIndexSort(GdbPcIndexInfoDTO dto) {

        List<GdbPcIndexVO> gdbPcIndexDTOList = dto.getGdbPcIndexDTOList();
        if(null != gdbPcIndexDTOList && gdbPcIndexDTOList.size() > 0 ){
            for (GdbPcIndexVO gdbPcIndexDTO : gdbPcIndexDTOList) {
                if(5 == gdbPcIndexDTO.getPageType() || 6 == gdbPcIndexDTO.getPageType()){
                    //更新gdb_merchant_floor_adpage表数据排序
                  this.baseMapper.update(null,new LambdaUpdateWrapper<BaseFloorAdpageEntity>()
                                            .set(BaseFloorAdpageEntity::getFloorAdpageOrder,gdbPcIndexDTO.getPageSort())
                                            .eq(BaseFloorAdpageEntity::getFloorType, CommonType.commonStatus.YES.getCode())
                                            .eq(gdbPcIndexDTO.getPageType()!=null,BaseFloorAdpageEntity::getPageType,gdbPcIndexDTO.getPageType())
                                            .eq(gdbPcIndexDTO.getFloorAdpageId()!=null,BaseFloorAdpageEntity::getFloorAdpageId,gdbPcIndexDTO.getFloorAdpageId()));
                }else{
                    //更新gdb_pc_index_config数据排序
                    basePcIndexConfigMapper.update(null,new LambdaUpdateWrapper<BasePcIndexConfigEntity>()
                                            .set(BasePcIndexConfigEntity::getPageSort,gdbPcIndexDTO.getPageSort())
                                            .eq(gdbPcIndexDTO.getPageType()!=null,BasePcIndexConfigEntity::getPageType,gdbPcIndexDTO.getPageType()));
                }
            }
            //存入redis
            RedisUtil.removeAll(RedisPrefixUtils.pcIndexConfig);
        }

        return ResponseResult.success(BizCodeEnume.OPERATION_SUCCESSFUL);
    }

    @Override
    public ResponseResult updateAdpageFloor(FloorListDTO param) {
        List<FloorDTO> updateFloorAdpage = Lists.newArrayList();
        List<FloorDTO> addFloorAdpage = Lists.newArrayList();

        for (FloorDTO floor : param.getFloorUpdateList()) {
            if (null == floor.getRightRec().getRightAdpageNavGoodsId()) {
                return ResponseResult.fail(BizCodeEnume.VALID_EXCEPTION.getCode(), BizCodeEnume.VALID_EXCEPTION.getMsg());

            }
            String str = floor.getRightRec().getRightAdpageNavGoodsId().replace('，', ',');
            floor.getRightRec().setRightAdpageNavGoodsId(str);
            //获取楼层增加代饿goodsId长度，如果超过8个，则不允许添加
            String[] arr;
            arr = str.split(",");
            if (arr.length > 8) {
                return ResponseResult.fail(BizCodeEnume.UPDATE_ADPAGE_Floor_MAX_ERROP.getCode(), BizCodeEnume.UPDATE_ADPAGE_Floor_MAX_ERROP.getMsg());

            } else if (arr.length == 0 || "".equals(arr[0])) {
                return ResponseResult.fail(BizCodeEnume.UPDATE_ADPAGE_Floor_MIN__ERROP.getCode(), BizCodeEnume.UPDATE_ADPAGE_Floor_MIN__ERROP.getMsg());

            }

            floor.setFloorAdpageName(JsonUtils.toString(floor.getFloorName()));
            floor.setLeftAdpageNav(JsonUtils.toString(floor.getLeftAdpageNavList()));
            floor.setRightHotRecommend(JsonUtils.toString(floor.getRightRec()));
            floor.setRightAdpageNav(JsonUtils.toString(floor.getRightAdpageNavList()));
            floor.setFloorUpdateTime(new Date());

            if (null == floor.getFloorAdpageId()) {
                addFloorAdpage.add(floor);
            } else {
                updateFloorAdpage.add(floor);
            }
        }

        if (!CollectionUtils.isEmpty(updateFloorAdpage)) {
            this.baseMapper.updateFloorAdpage(updateFloorAdpage);
        }
        if (!CollectionUtils.isEmpty(addFloorAdpage)) {
            this.baseMapper.insertFloorAdpage(addFloorAdpage);
        }
        RedisUtil.removeAll("PC_INDEX_CONFIG_TEST");
        return ResponseResult.success();
    }

    @Override
    public ResponseResult queryWebIndexFloor(Integer floorAdpageId) {
        BaseFloorAdpageEntity floorAdpage = this.baseMapper.selectById(floorAdpageId);
        return ResponseResult.success(floorAdpage);
    }

    @Override
    public ResponseResult queryWebIndexFloorList() {
        List<BaseFloorAdpageEntity> results = this.baseMapper.selectList(new LambdaQueryWrapper<BaseFloorAdpageEntity>()
                                             .orderByAsc(BaseFloorAdpageEntity::getFloorAdpageOrder));
        return ResponseResult.success(results);
    }


}




